package com.choichumgming.rentcat.service.impl;

import com.choichumgming.rentcat.mapper.AmsArticleMapper;
import com.choichumgming.rentcat.model.AmsArticle;
import com.choichumgming.rentcat.model.AmsArticleExample;
import com.choichumgming.rentcat.model.UmsMember;
import com.choichumgming.rentcat.service.AmsArticleService;
import com.choichumgming.rentcat.service.UmsMemberFeignApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
@Service
public class AmsArticleServiceImpl implements AmsArticleService {
    @Autowired
    private AmsArticleMapper amsArticleMapper;

    @Resource
    private UmsMemberFeignApi umsMemberFeignApi;


    /**
     * @Description : 添加文章
     *
     * @param article
     * @Return : int
     * @Author : choichumgming
     * @Date : 27/2/2020 23:52
    */
    @Override
    public int add(AmsArticle article) {
//        UmsMember currentMember = umsMemberFeignApi.getCurrentUser().getData();
//        article.setUserId(currentMember.getUserId());
        article.setReadCount(0);
        return amsArticleMapper.insert(article);
    }

    /**
     * @Description : 删除文章
     *
     * @param articleId
     * @Return : int
     * @Author : choichumgming
     * @Date : 27/2/2020 23:53
    */
    @Override
    public int delete(Long articleId) {
        AmsArticleExample example = new AmsArticleExample();
        example.createCriteria().andArticleIdEqualTo(articleId);
        return amsArticleMapper.deleteByExample(example);
    }

    /**
     * @Description : 修改文章
     *
     * @param articleId
     * @param article
     * @Return : int
     * @Author : choichumgming
     * @Date : 27/2/2020 23:53
    */

    @Override
    public int update(Long articleId, AmsArticle article) {
        article.setArticleId(null);
        UmsMember currentMember = umsMemberFeignApi.getByUserId(article.getUserId()).getData();
        AmsArticleExample example = new AmsArticleExample();
        example.createCriteria().andUserIdEqualTo(currentMember.getUserId()).andArticleIdEqualTo(articleId);
        return amsArticleMapper.updateByExampleSelective(article,example);
    }

    /**
     * @Description : 获得指定用户所有文章
     *
     * @param userId
     * @Return : java.util.List<com.choichumgming.rentcat.model.AmsArticle>
     * @Author : choichumgming
     * @Date : 27/2/2020 23:53
    */
    @Override
    public List<AmsArticle> list(Long userId) {
        AmsArticleExample example = new AmsArticleExample();
        example.createCriteria().andUserIdEqualTo(userId);
        return amsArticleMapper.selectByExample(example);
    }

    /**
     * @Description : 获取文章详情
     *
     * @param articleId
     * @Return : com.choichumgming.rentcat.model.AmsArticle
     * @Author : choichumgming
     * @Date : 27/2/2020 23:53
    */

    @Override
    public AmsArticle getItem(Long articleId) {
        AmsArticleExample example = new AmsArticleExample();
        example.createCriteria().andArticleIdEqualTo(articleId);
        List<AmsArticle> articleList = amsArticleMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(articleList)){
            return articleList.get(0);
        }
        return null;
    }

    /**
     * @Description : 根据商品类别获得文章
     *
     * @param producCategorId
     * @Return : java.util.List<com.choichumgming.rentcat.model.AmsArticle>
     * @Author : choichumgming
     * @Date : 27/2/2020 23:54
    */
    @Override
    public List<AmsArticle> getAmsArticleList(Long producCategorId) {
        AmsArticleExample example = new AmsArticleExample();
        example.createCriteria().andProductCategoryIdEqualTo(producCategorId);
        return amsArticleMapper.selectByExample(example);
    }
    /**
     * @Description : 根据标题关键字搜索文章内容
     *
     * @param keyword
     * @Return : java.util.List<com.choichumgming.rentcat.model.AmsArticle>
     * @Author : choichumgming
     * @Date : 1/3/2020 15:52
     */

    @Override
    public List<AmsArticle> getAmsArticleByKeyword(String keyword) {
        AmsArticleExample articleExample = new AmsArticleExample();
        AmsArticleExample.Criteria criteria = articleExample.createCriteria();
        if(!StringUtils.isEmpty(keyword)){
            criteria.andTitleLike("%" + keyword + "%");
        }
        return amsArticleMapper.selectByExample(articleExample);
    }

    /**
     * @Description : 批量删除文章
     *
     * @param articleIdList
     * @Return : int
     * @Author : choichumgming
     * @Date : 22/3/2020 17:02
     */
    @Override
    public int deleteList(List<Long> articleIdList) {
        for(Long articleId: articleIdList){
            amsArticleMapper.deleteByPrimaryKey(articleId);
        }
        return 1;
    }
}
